package com.prs.crm.action.cdxh;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.cdxh.CrmCustomer;
import com.prs.crm.domain.cdxh.SerialNumberss;
import com.prs.crm.domain.crm.CustomerType;
import com.prs.crm.domain.sys.Code;
import com.prs.crm.domain.sys.CodeCommon;
import com.prs.crm.exception.InValidDeleteException;
import com.prs.crm.service.cdxh.CrmCustomerService;
import com.prs.crm.service.cdxh.SerialNumberssService;
import com.prs.crm.service.crm.CustomerTypeService;
import com.prs.crm.service.sys.CodeCommonService;
import com.prs.crm.service.sys.CodeService;

public class CrmCustomerAction extends BaseAction {
	private static final long serialVersionUID = 1L;

	private SerialNumberssService serialNumberssService;
	private CrmCustomer crmcustomer;
	private CodeService codeService;
	private CrmCustomerService crmCustomerService;
	private List<CustomerType> types;
	private CustomerTypeService customerTypeService;
	private String Encode;
	private CodeCommon codeCommon;
	private Code code;
	private String style;
	private CodeCommonService codeCommonService;
	private Collection<CodeCommon> codelist;
	private Map<String, String> alist;
	private Integer[] crmcustomerIds;
	private PaginationSupport pagination;
	private CustomerType customerType;
	@Action("viewCrmCustomer")
	public String view() {
		return SUCCESS;
	}
	
	
	@Action("selectProductCustomer")
	public String selectProductCustomer() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				CrmCustomer.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getCrmCustomerService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Action("listCrmCustomer")
	public String listCrmCustomer() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				CrmCustomer.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getCrmCustomerService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Clew(results = { "success", "none" })
	@Action(value = "saveCrmCustomer", results = {
			@Result(name = "success", type = "redirect", location = "editCrmCustomer"),
			@Result(name = "none", type = "redirect", location = "alertCrmCustomer"),
			@Result(name = "error", type = "chain", location = "editCrmCustomer") })
	public String save() {
		if (crmcustomer.getCusName() == null || "".equals(crmcustomer.getCusName().trim())) {
			this.addActionError("客户名称不能为空");
			return ERROR;
		}
		if (crmcustomer.getCusStyle() == null) {
			this.addActionError("客户类型不能为空");
			return ERROR;
		}
		if (!(isnull(crmcustomer.getCusAddress()))) {
			this.addActionError("地址不能为空");
			return ERROR;
		}
		if (isnull(crmcustomer.getCusPhone1())) {
			if (istel(crmcustomer.getCusPhone1())) {
				this.addActionError("电话1 格式不对！请重新输入");
				return ERROR;
			}
		}
		if (isnull(crmcustomer.getCusPhone2())) {
			if (istel(crmcustomer.getCusPhone2())) {
				this.addActionError("电话2  格式不对！请重新输入");
				return ERROR;
			}
		}
		if ( !"".equals(crmcustomer.getCusFax().trim())
				&&!ToolKits.isInteger(crmcustomer.getCusFax())) {
			this.addActionError("传真只能为数字");
			return ERROR;
		}
		if (isnull(crmcustomer.getCusEmail())) {
			if (emailFormat(crmcustomer.getCusEmail())) {
				this.addActionError("邮箱格式不对!请重新输入");
				return ERROR;
			}
		}
		if (crmcustomer.getId() == null) {
			
			/**
			 * 编码产生
			 */
			String proCode="KH2010O1";
			SerialNumberss snObject=this.getSerialNumberssService().getSerialNumberss("客户");
			
			if(snObject!=null){
				proCode=snObject.getStyNumberCode();
				String  prosCode[]= (snObject.getStyNumberCode()).split("O");
				int overNums=Integer.parseInt(prosCode[1])+1;
				String putValues=prosCode[0]+"O"+overNums;
				snObject.setStyNumberCode(putValues);
				this.getSerialNumberssService().setNumCode(snObject);
			}else{		
				SerialNumberss snsObject=new SerialNumberss();
				snsObject.setStyCategory("客户");			
				snsObject.setStyNumberCode("KH2010O2");
				this.getSerialNumberssService().save(snsObject);
			}
				crmcustomer.setCusCode(proCode);
			
				crmcustomer.setCusState1(Integer.valueOf(Constrains.TASK_AUDITING));
				this.getCrmCustomerService().save(crmcustomer);
			this.setPrompt("录入客户成功");
		} else {
			this.getCrmCustomerService().save(crmcustomer);
			this.setPrompt("变更客户成功");
			return NONE;
		}

		return SUCCESS;
	}
	public Code getCode() {
		return code;
	}
	
	public void setCode(Code code) {
		this.code = code;
	}
	@Action("editCrmCustomer")
	public String edit() {
		
		this.types = this.getCustomerTypeService().getAllCustomerType();
		return SUCCESS;
	}
	@Action("alertCrmCustomer")
	public String alertCrmCustomer() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				CrmCustomer.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getCrmCustomerService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action(value = "removeCrmCustomer", results = {
			@Result(name = "success", type = "redirect", location = "alertCrmCustomer"),
			@Result(name = "error", type = "chain", location = "alertCrmCustomer") })
	public String remove() {
		try {
			if (crmcustomerIds == null) {
				this.addActionError("请选择要删除的客户");
				return ERROR;
			}
			this.getCrmCustomerService().batchRemove(crmcustomerIds);
			this.setPrompt("删除成功");
			return SUCCESS;
		} catch (InValidDeleteException e) {
			return ERROR;
		}
	}
	
	public CrmCustomer getCrmcustomer() {
		return crmcustomer;
	}
	public CodeService getCodeService() {
		return codeService;
	}
	public void setCodeService(CodeService codeService) {
		this.codeService = codeService;
	}
	public CodeCommon getCodeCommon() {
		return codeCommon;
	}

	public void setCodeCommon(CodeCommon codeCommon) {
		this.codeCommon = codeCommon;
	}
	

	public CrmCustomerService getCrmCustomerService() {
		return crmCustomerService;
	}
	public void setCrmCustomerService(CrmCustomerService crmCustomerService) {
		this.crmCustomerService = crmCustomerService;
	}
	public Map<String, String> getAlist() {
		return alist;
	}
	public void setAlist(Map<String, String> alist) {
		this.alist = alist;
	}
	public CodeCommonService getCodeCommonService() {
		return codeCommonService;
	}
    
	public String getStyle() {
		return style;
	}
	public void setStyle(String style) {
		this.style = style;
	}
	public CustomerType getCustomerType() {
		return customerType;
	}
	public void setCustomerType(CustomerType customerType) {
		this.customerType = customerType;
	}
	public void setCodeCommonService(CodeCommonService codeCommonService) {
		this.codeCommonService = codeCommonService;
	}

	public Collection<CodeCommon> getCodelist() {
		return codelist;
	}

	public void setCodelist(Collection<CodeCommon> codelist) {
		this.codelist = codelist;
	}
	public List<CustomerType> getTypes() {
		return types;
	}

	public void setTypes(List<CustomerType> types) {
		this.types = types;
	}

	public CustomerTypeService getCustomerTypeService() {
		return customerTypeService;
	}

	public void setCustomerTypeService(CustomerTypeService customerTypeService) {
		this.customerTypeService = customerTypeService;
	}
	
	public String getEncode() {
		return Encode;
	}

	public void setEncode(String encode) {
		Encode = encode;
	}

	public void setCrmcustomer(CrmCustomer crmcustomer) {
		this.crmcustomer = crmcustomer;
	}
	public void setCrmcustomerIds(Integer[] crmcustomerIds) {
		this.crmcustomerIds = crmcustomerIds;
	}
	public Integer[] getCrmcustomerIds() {
		return crmcustomerIds;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public static String Increase(String backupNum) {
		String Inc = "";
		int lengths = backupNum.length();
		String lengthss = (Integer.parseInt(backupNum) + 1) + "";
		int increase = (lengthss.trim()).length();
		if (lengths != increase) {
			String In = "";
			for (int i = 0; i < lengths - increase; i++) {
				In = In + "0";
			}
			In = (In + lengthss).trim();
			Inc = In;
		} else {
			Inc = lengthss;
		}
		return Inc.trim();
	}
	
	public boolean emailFormat(String email) {
		boolean tag = false;
		final String pattern1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
		final Pattern pattern = Pattern.compile(pattern1);
		final Matcher mat = pattern.matcher(email);
		if (!mat.find()) {
			tag = true;
		}
		return tag;
	}

	public boolean istel(String str) {
		boolean tag = false;  
		final String pattern1 = "^(([0-9]{3,4})|[0-9]{3,4}-)?[0-9]{7,8}$";
		final Pattern pattern = Pattern.compile(pattern1);
		final Matcher mat = pattern.matcher(str);
		if (!mat.find()) {
			tag = true;
		}
		return tag;
	}

	public boolean isnull(String str) {
		boolean s=true;
		if (str.trim().equals("") || str == null) {
			s= false;
		}
		return s;
	}


	public SerialNumberssService getSerialNumberssService() {
		return serialNumberssService;
	}


	public void setSerialNumberssService(SerialNumberssService serialNumberssService) {
		this.serialNumberssService = serialNumberssService;
	}
	
}
